package com.jjb.www.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.http.api.HomeMessageListApi;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.ui.adapter.MessageListAdapter;
import com.jjb.www.widget.CommonRefreshView;

import java.util.List;

import okhttp3.Call;

/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 首页通知公告
 */
public final class MessageListActivity extends AppActivity implements BaseAdapter.OnItemClickListener {
    private CommonRefreshView mRefreshView;
    private MessageListAdapter mAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void initView() {
        mRefreshView = findViewById(R.id.refreshView);
        mAdapter = new MessageListAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<HomeMessageListApi.Bean>() {
            @Override
            public AppAdapter<HomeMessageListApi.Bean> getAdapter() {
                return mAdapter;
            }

            @Override
            public void loadData(int p, HttpCallback<HttpListData<HomeMessageListApi.Bean>> callback) {

                EasyHttp.get(MessageListActivity.this)
                        .api(new HomeMessageListApi()
                                .setPage(p)
                                .setPageSize(mAdapter.getSize())
                        )
                        .request(new HttpCallback<HttpListData<HomeMessageListApi.Bean>>(MessageListActivity.this) {
                            @Override
                            public void onSucceed(HttpListData<HomeMessageListApi.Bean> result) {
                                callback.onSucceed(result);

                            }

                            @Override
                            public void onFail(Exception e) {
                                super.onFail(e);
                                callback.onFail(e);
                            }

                            @Override
                            public void onEnd(Call call) {
                                super.onEnd(call);
                                callback.onEnd(call);
                            }
                        });
            }

            @Override
            public void onRefreshSuccess(List<HomeMessageListApi.Bean> list, int listCount) {

            }

            @Override
            public void onRefreshFailure() {

            }

            @Override
            public void onLoadMoreSuccess(List<HomeMessageListApi.Bean> loadItemList, int loadItemCount) {

            }

            @Override
            public void onLoadMoreFailure() {

            }
        });
        mRefreshView.initData();
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        Intent intent = new Intent();
        intent.putExtra("title",mAdapter.getItem(position).getTitle());
        intent.putExtra("path",mAdapter.getItem(position).getPath());
        intent.setClass(this,MessageDetitleActivity.class);
        startActivity(intent);
    }
}